package com.yft.serve.user;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author lzt
 * @date 2024/6/3 9:21
 */
@RestController
@RequestMapping("/hh")
@Slf4j
public class UserController {

    @Resource
    public RestTemplate restTemplate;

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
    @Resource
    LoadBalancerClient loadBalancerClient;

    @GetMapping("/ribbon")
    public String ribbon(){
        ServiceInstance choose = loadBalancerClient.choose("spring-cloud-order-service");
        String url = String.format("http://%s:%s", choose.getHost(), choose.getPort() + "/hello");
        System.out.println(":"+url);
        return restTemplate.getForObject(url,String.class);
    }

    @GetMapping("/a")
    public String a(){
        log.info("执行了接口a");
        return "ok";
    }
    @GetMapping("/b")
    public String b(){
        try {
            int i = 10/0;
        }catch (Exception e){
            e.printStackTrace();
            log.error("错误信息", e);
        }
        return "ok";
    }
    @Scheduled(cron = "0/5 * * * * *")
    public void execute(){
        log.info("定时任务执行了,当前时间："+new Date()+"; 随机数："+Math.ceil(Math.random()*1000000));
    }


    @Scheduled(cron = "0/2 * * * * *")
    public void execute1(){
        log.info("==============当前时间"+new Date()+"================");
        List<String> xingList = new ArrayList<>();
        xingList.add("兆");
        xingList.add("钱");
        xingList.add("赵");
        xingList.add("孙");
        xingList.add("李");
        xingList.add("周");
        xingList.add("张");
        xingList.add("吴");
        xingList.add("郑");
        xingList.add("王");
        xingList.add("朱");
        xingList.add("赢");
        String mingList ="这是一个美好的今天我走了刺客阳光命令明媚好的 到南京市时有盆友约去瞎逛勾留了一日第二日早上便须渡海到浦口中午进入车内北去父亲由于事忙本已说定不送我叫宾馆里一个熟悉的茶房陪着我同去他再三叮嘱茶房甚为细心但他总算不安心怕茶房不合适颇彷徨了一会其实我那一年已二十岁北京市已往来过两三次是沒有什么关键的了他彷徨了一会总算决策或是自身送我要去我两三回劝他无须去他只说没事儿她们去不太好";
        int xingSize = xingList.size();
        Random random = new Random();
        int currentXing1 = random.nextInt(mingList.length());
        int currentXing2 = random.nextInt(mingList.length());
        String ming = "" + xingList.get(random.nextInt(xingSize))+ mingList.substring(currentXing1,currentXing1+1)+mingList.substring(currentXing2,currentXing2+1);
        log.info("欢迎你："+ming+",来到了我的世界");
        log.info("整合姓名完成");
        log.info("======================结束了=========================");
    }
}
